package integration.data.task.api.transformation.impl;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import integration.data.task.api.transformation.Transformer;
import integration.modle.exception.IntegrationValidException;
import io.github.bigbird0101.datatransfer.model.element.Column;
import io.github.bigbird0101.datatransfer.model.element.StringColumn;
import io.github.bigbird0101.datatransfer.record.Record;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Map;

import static integration.data.task.api.constant.Constant.*;

/**
 * 数据脱敏
 */
@Slf4j
public class DataMasking extends Transformer {

    public DataMasking() {
        setTransformerName(DATA_MASKING);
    }

    @Override
    public Record evaluate(Record record, Map<String, Object> paras) {

        int columnIndex;
        String code;
        try {
            if (paras.size() != 2) {
                throw new IntegrationValidException("脱敏参数必须是2个");
            }

            columnIndex = (Integer) paras.get(COLUMN);
            code = (String) paras.get(OPERATOR);

        } catch (Exception e) {
            throw new IntegrationValidException("转换参数异常" + "paras:" + Arrays.asList(paras).toString() + " => " + e.getMessage());
        }

        Column column = record.getColumn(columnIndex);

        try {
            String oldStr = column.asString();
            String newStr = null;
            if ("PHONE_NUMBER".equalsIgnoreCase(code)) {
                // 手机号
                newStr = DesensitizedUtil.mobilePhone(oldStr);
            } else if ("ID_NUMBER".equalsIgnoreCase(code)) {
                // 身份证号
                newStr = DesensitizedUtil.idCardNum(oldStr, 0, 4);
            } else if ("BANK_CARD_NUMBER".equalsIgnoreCase(code)) {
                // 银行卡号
                newStr = DesensitizedUtil.bankCard(oldStr);
            } else if ("MAILBOX".equalsIgnoreCase(code)) {
                // 邮箱
                newStr = DesensitizedUtil.email(oldStr);
            } else if ("ADDRESS".equalsIgnoreCase(code)) {
                // 地址 大于12位显示前6位，不足12位显示不超过50%
                if (oldStr != null) {
                    int hideLen = 6;
                    int length = oldStr.length();
                    if (length <= 12) {
                        hideLen = length - (length / 2);
                    } else {
                        hideLen = length - hideLen;
                    }
                    newStr = DesensitizedUtil.address(oldStr, hideLen);
                }
            } else if ("FULL_NAME".equalsIgnoreCase(code)) {
                // 姓名
                newStr = DesensitizedUtil.chineseName(oldStr);
            } else if ("CAR_NUMBER".equalsIgnoreCase(code)) {
                // 车牌号
                newStr = DesensitizedUtil.carLicense(oldStr);
            } else if ("LANDLINE_NUMBER".equalsIgnoreCase(code)) {
                // 座机号
                newStr = DesensitizedUtil.fixedPhone(oldStr);
            } else if ("IP".equalsIgnoreCase(code)) {
                // TODO: ip
                newStr = defaultMask(oldStr);
            } else if ("POSTAL_CODE".equalsIgnoreCase(code)) {
                //TODO: 邮政编码
                newStr = defaultMask(oldStr);
            } else if ("MAC_ADDRESS".equalsIgnoreCase(code)) {
                // TODO: mac 地址
                newStr = defaultMask(oldStr);
            } else if ("COMPANY_NAME".equalsIgnoreCase(code)) {
                // TODO: 公司名
                newStr = defaultMask(oldStr);
            } else if ("NATION".equalsIgnoreCase(code)) {
                // TODO: 民族
                newStr = defaultMask(oldStr);
            } else if ("CONSTELLATION".equalsIgnoreCase(code)) {
                // TODO: 星座
                newStr = defaultMask(oldStr);
            } else if ("GENDER".equalsIgnoreCase(code)) {
                // TODO: 性别
                newStr = defaultMask(oldStr);
            } else if ("NATIONALITY".equalsIgnoreCase(code)) {
                //TODO: 国籍
                newStr = defaultMask(oldStr);
            } else {
                throw new IntegrationValidException("不支持的脱敏类型:" + code);
            }

            record.setColumn(columnIndex, new StringColumn(newStr));

        } catch (Exception e) {
            throw new IntegrationValidException("过滤运行异常", e);
        }
        return record;
    }


//    private String defaultMask(String oldStr, int startIndex, int endIndex) {
//        if (StrUtil.isBlank(oldStr)) {
//            return StrUtil.EMPTY;
//        }
//        return StrUtil.hide(oldStr, startIndex, endIndex);
//    }

    private String defaultMask(String oldStr, int startIndex) {
        if (StrUtil.isBlank(oldStr)) {
            return StrUtil.EMPTY;
        }
        return StrUtil.hide(oldStr, startIndex, oldStr.length());
    }

    private String defaultMask(String oldStr) {
        return defaultMask(oldStr, 0);
    }
}
